<?php

/**
 * @author gbfun <gbfun@163.com>  2018-04-15 20:49
 * http://www.wuweiyou.com
 */

class db_table_background_good_category extends db_table_good_category
{    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    public function add($good_category_info)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
        
        $sql = '
            INSERT INTO ' . $this->_full_name . '
                (`parent_id`, `name`, `order`, `status`, `create_time`, `update_time`)
            VALUES
                (?, ?, ?, ?, ?, ?)
        ';
        
        $bind = array(
            $good_category_info['parent_id'],
            htmlspecialchars($good_category_info['name']),
            $good_category_info['order'],
            $good_category_info['status'],
            $good_category_info['create_time'],
            $good_category_info['update_time'],
        );
        
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
        if($result){
            $result = $write_db->last_insert_id();
        }
        //var_dump($result); exit();
        
        return $result;
    }
    
    public function update($good_category_info)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            UPDATE ' . $this->_full_name . '
            SET
                `parent_id` = ?,
                `name` = ?,
                `order` = ?,
                `status` = ?,
                `update_time` = ?
            WHERE `id` = ?
        ';
    
        $bind = array(
            $good_category_info['parent_id'],
            htmlspecialchars($good_category_info['name']),
            $good_category_info['order'],
            $good_category_info['status'],
            $good_category_info['update_time'],

            $good_category_info['id'],
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
}

